package com.example.yaja.yaja.controller;

import com.example.yaja.yaja.entity.News;
import com.example.yaja.yaja.entity.Travels;
import com.example.yaja.yaja.service.NewsService;
import com.example.yaja.yaja.service.TravelsService;
import com.example.yaja.yaja.upload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by Zero on 2017/12/6.
 */
@RestController
@RequestMapping("/picture")
public class PictureController {


    @Autowired
    TravelsService travelsService;
    @Autowired
    NewsService newsService;

    /**
     * 上传图片
     *
     * @return
     */
    @RequestMapping(value = "/travels/{travelsId}", method = RequestMethod.POST)
    public void photoUpload(@RequestParam(value = "file") MultipartFile multipartFile, @PathVariable int travelsId, HttpServletRequest httpServletRequest) throws IOException {
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/images/";
        //生成随机字符串，用于图片命名
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 获得文件类型
        String fileType = multipartFile.getContentType();
        // 获得文件后缀名称
        String imageName = fileType.substring(fileType.indexOf("/") + 1);
        // 原名称
//        String fileName = file.getOriginalFilename();
        // 新名称
        String newFileName = uuid + "." + imageName;
        try {
            FileUtils.upload(multipartFile.getBytes(), path, newFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String imgName = newFileName;
        Travels travels = travelsService.findTravelsById(travelsId);
        travels.setPicture("/images/" + imgName);
        travelsService.updateTravels(travels);
    }

    /**
     * 上传图片
     *
     * @return
     */
    @RequestMapping(value = "/news/{newsId}", method = RequestMethod.POST)
    public void photoUpload1(@RequestParam(value = "file") MultipartFile multipartFile, @PathVariable int newsId, HttpServletRequest httpServletRequest) throws IOException {
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/images/";
        //生成随机字符串，用于图片命名
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 获得文件类型
        String fileType = multipartFile.getContentType();
        // 获得文件后缀名称
        String imageName = fileType.substring(fileType.indexOf("/") + 1);
        // 原名称
//        String fileName = file.getOriginalFilename();
        // 新名称
        String newFileName = uuid + "." + imageName;
        try {
            FileUtils.upload(multipartFile.getBytes(), path, newFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String imgName = newFileName;
        News news = newsService.findNewsById(newsId);
        news.setPicture("/images/" + imgName);
        newsService.updateNews(news);
    }


    /**
     * 上传图片
     *
     * @return
     */
    @RequestMapping(value = "/notice/{newsId}", method = RequestMethod.POST)
    public void photoUpload2(@RequestParam(value = "file") MultipartFile multipartFile, @PathVariable int newsId, HttpServletRequest httpServletRequest) throws IOException {
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/images/";
        //生成随机字符串，用于图片命名
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 获得文件类型
        String fileType = multipartFile.getContentType();
        // 获得文件后缀名称
        String imageName = fileType.substring(fileType.indexOf("/") + 1);
        // 新名称
        String newFileName = uuid + "." + imageName;
        try {
            FileUtils.upload(multipartFile.getBytes(), path, newFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String imgName = newFileName;
        News news = newsService.findNewsById(newsId);
        news.setPicture("/images/" + imgName);
        newsService.updateNews(news);
    }
}
